//package com.xnyy.lottery.security;
//
//import cn.hutool.core.util.StrUtil;
//import com.xnyy.lottery.util.JwtUtils;
//import io.jsonwebtoken.Claims;
//import io.jsonwebtoken.JwtException;
//import lombok.Setter;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
//
//import javax.annotation.Resource;
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//@Setter
//public class JwtAuthenticationFilter extends BasicAuthenticationFilter {
//
//    @Resource
//    private JwtUtils jwtUtils;
//    @Resource
//    private UserDetailsServiceImpl userDetailService;
//
//
//    public JwtAuthenticationFilter(AuthenticationManager authenticationManager) {
//        super(authenticationManager);
//    }
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
//        String jwt = request.getHeader(jwtUtils.getHeader());
//        // 这里如果没有jwt，继续往后走，因为后面还有鉴权管理器等去判断是否拥有身份凭证，所以是可以放行的
//        // 没有jwt相当于匿名访问，若有一些接口是需要权限的，则不能访问这些接口
//        if (StrUtil.isBlankOrUndefined(jwt)) {
//            chain.doFilter(request, response);
//            return;
//        }
//
//        Claims claim = jwtUtils.getClaimsByToken(jwt);
//        if (claim == null) {
//            throw new JwtException("token 异常");
//        }
//        if (jwtUtils.isTokenExpired(claim)) {
//            throw new JwtException("token 已过期");
//        }
//        //TODO 每次请求校验服务器是否存在指定token
//
//        String username = claim.getSubject();
//        // 构建UsernamePasswordAuthenticationToken,这里密码为null，是因为提供了正确的JWT,实现自动登录
//        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, null, userDetailService.getUserAuthority());
//        SecurityContextHolder.getContext().setAuthentication(token);
//
//        chain.doFilter(request, response);
//
//    }
//}
